package com.jilian.mydemo.modul.order.ui;

import android.app.Dialog;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.base.BaseFragment;
import com.jilian.mydemo.common.dto.OrderDto;
import com.jilian.mydemo.common.dto.OrderListDto;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.order.adapter.OneOrderAdapter;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.DisplayUtil;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.PinziDialogUtils;
import com.jilian.mydemo.utils.ToastUitl;
import com.jilian.mydemo.views.RecyclerViewSpacesItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class OneOrderFragment extends BaseFragment implements CustomItemClickListener, OneOrderAdapter.OneOrderListener {
    private RecyclerView recyclerview;
    private OneOrderAdapter adapter;
    private LinearLayoutManager linearLayoutManager;
    private List<OrderDto> datas;
    private SmartRefreshLayout srHasData;
    private SmartRefreshLayout srNoData;
    private OrderViewModel orderViewModel;

    @Override
    protected void loadData() {
        showLoadingDialog();
        pageNum = 1;
        queryOrderList();
    }

    @Override
    protected void createViewModel() {
        orderViewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_oneorder;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
        srHasData = (SmartRefreshLayout) view.findViewById(R.id.sr_has_data);
        srNoData = (SmartRefreshLayout) view.findViewById(R.id.sr_no_data);
        srHasData.setVisibility(View.GONE);
        srNoData.setVisibility(View.VISIBLE);
        srNoData.setEnableLoadMore(false);
        linearLayoutManager = new LinearLayoutManager(getParentFragment().getActivity());
        recyclerview.setLayoutManager(linearLayoutManager);
        datas = new ArrayList<>();
        HashMap<String, Integer> map = new HashMap<>();
        map.put(RecyclerViewSpacesItemDecoration.TOP_DECORATION, DisplayUtil.dip2px(getParentFragment().getActivity(), 10));//上间距
        recyclerview.addItemDecoration(new RecyclerViewSpacesItemDecoration(map));
        adapter = new OneOrderAdapter(getParentFragment().getActivity(), datas, this, this);
        recyclerview.setAdapter(adapter);

    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        srHasData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                queryOrderList();
            }
        });
        srHasData.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum++;
                queryOrderList();
            }
        });
        srNoData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                queryOrderList();
            }
        });
    }

    private Integer pageNum = 1;
    private Integer pageSize = 20;

    /**
     * 获取订单列表
     */
    private void queryOrderList() {
        orderViewModel.queryOrderList(pageNum, pageSize, 2,queryKey);
        orderViewModel.getOrderliveData().observe(this, new Observer<BaseDto<OrderListDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<OrderListDto> dto) {
                hideLoadingDialog();
                srNoData.finishRefresh();
                srHasData.finishRefresh();
                srHasData.finishLoadMore();
                //有数据
                if (dto.isSuccess() && EmptyUtils.isNotEmpty(dto.getData().getList())) {
                    srNoData.setVisibility(View.GONE);
                    srHasData.setVisibility(View.VISIBLE);
                    if (pageNum == 1) {
                        datas.clear();
                    }
                    datas.addAll(dto.getData().getList());
                    adapter.notifyDataSetChanged();
                } else {
                    //说明是上拉加载
                    if (pageNum > 1) {
                        pageNum--;
                    } else {
                        //第一次就没数据
                        srNoData.setVisibility(View.VISIBLE);
                        srHasData.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(getParentFragment().getActivity(), OneOrderDetailActivity.class);
        intent.putExtra("order",datas.get(position));
        startActivity(intent);
    }

    @Override
    public void grabSingle(int position) {
        showTipsDialog(position);
    }

    /**
     * 提示对话框
     */
    private void showTipsDialog(int position) {

        if (!("1".equals(datas.get(position).getIsLeader()))&&"1".equals(datas.get(position).getIsMore())) {
            ToastUitl.showImageToastTips("只有队长可以操作");
            return;
        }
        Dialog dialog = PinziDialogUtils.getDialogNotTouchOutside(getParentFragment().getActivity(), R.layout.dialog_confirm);
        TextView tvTitle = (TextView) dialog.findViewById(R.id.tv_title);
        TextView tvContent = (TextView) dialog.findViewById(R.id.tv_content);
        tvTitle.setText("抢单确认");
        tvContent.setText("确定抢单吗？");
        TextView tvNo = (TextView) dialog.findViewById(R.id.tv_no);
        TextView tvOk = (TextView) dialog.findViewById(R.id.tv_ok);

        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                grabOrder(position);
            }
        });
        tvNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();


    }

    /**
     * 抢单
     * @param position
     */
    private void grabOrder(int position){
        showLoadingDialog();
        orderViewModel.grabOrder(datas.get(position).getId());
        orderViewModel.getGrabliveData().observe(this, new Observer<BaseDto>() {
            @Override
            public void onChanged(@Nullable BaseDto dto) {
                hideLoadingDialog();
                if(dto.isSuccess()){
                    ToastUitl.showImageToastSuccess("抢单成功");
                    datas.remove(position);
                    adapter.notifyDataSetChanged();
                }
                else{
                    ToastUitl.showImageToastTips(dto.getMsg());
                }

            }
        });

    }
}
